package cn.yunyichina.provider.bed.thread;

import cn.yunyichina.provider.bed.dao.InpatientRecordDao;
import cn.yunyichina.provider.bed.dao.OutpatientSummaryDao;
import cn.yunyichina.provider.bed.entity.MedicalCard;
import cn.yunyichina.provider.bed.entity.OutpatientSummary;
import cn.yunyichina.provider.bed.vo.BedVo;
import cn.yunyichina.provider.framework.common.PKGenerator;
import cn.yunyichina.provider.framework.data.SpringContextHandler;
import cn.yunyichina.provider.framework.dubbo.service.BaseService;
import cn.yunyichina.provider.framework.exception.CallException;
import cn.yunyichina.provider.iface.entity.base.Org;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.provider.iface.entity.hisiface.bed.OutSummaryVoResponse;
import cn.yunyichina.utils.commons.DateUtils;
import cn.yunyichina.utils.convert.JsonUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zd on 2016/12/17.
 */
public class OutpatientSummaryRunnable implements  Runnable{
    private Logger logger = LoggerFactory.getLogger(OutpatientSummaryRunnable.class);

    private OutSummaryVoResponse outSummary;
    private BedVo requestVo;

    public OutpatientSummaryRunnable(OutSummaryVoResponse outSummary,BedVo requestVo) {
        this.requestVo = requestVo;
        this.outSummary = outSummary;
    }

    @Override
    public void run() {
        OutpatientSummaryDao outpatientSummaryDao = SpringContextHandler.getBean(OutpatientSummaryDao.class);
        OutpatientSummary outpatientSummary = outpatientSummaryDao.selectPatientIdAndAdmissionNo(requestVo.getPatientId(),requestVo.getAdmissionNo(),requestVo.getHospitalCode());

        if (outpatientSummary == null){
            MedicalCard medicalCard = null;
            if (StringUtils.isNotBlank(requestVo.getMedicalCardId())){
                //查询绑定卡
                JSONObject medicalParam = new JSONObject();
                medicalParam.put("id", requestVo.getMedicalCardId());
                logger.info("【出院小结保存线程处理-查询绑定卡信息】, yyt.base.medicalcard.by.id.get request=" + JSON.toJSONString(medicalParam));
                Response medicalResponse = BaseService.callHumpFromInternal("yyt.base.medicalcard.by.id.get", JsonUtils.toJsonString(medicalParam));
                logger.info("【出院小结清单保存线程处理-查询绑定卡信息】, yyt.base.medicalcard.by.id.get response=" + JSONObject.toJSONString(medicalResponse));
                if (!"0".equals(medicalResponse.getResultCode())) {
                    throw new CallException("14010","找不到绑定卡信息 by id=" + requestVo.getMedicalCardId());
                }
                medicalCard = JsonUtils.parseObject(medicalResponse.getResult(), MedicalCard.class);
            }

            // 查询组织信息-----------------------------------------------start
            Map<String, String> orgParamMap = new HashMap<String, String>();
            orgParamMap.put("org_code", requestVo.getHospitalCode());
            logger.info("【出入院-出院小结-查询组织信息】, yyt.base.branchhospital.org.list request=" + JSON.toJSONString(orgParamMap));
            Response orgResponse = BaseService.callUnderlineFromInternal("yyt.base.branchhospital.org.list", JSON.toJSONString(orgParamMap));
            logger.info("【出入院-出院小结-查询组织信息】, yyt.base.branchhospital.org.list response=" + JSONObject.toJSONString(orgResponse));
            if (!"0".equals(orgResponse.getResultCode())) {
                throw new CallException("14010","找不到机构信息 by org_code=" + requestVo.getHospitalCode());
            }
            List<Org> orgList = JsonUtils.parseArray(orgResponse.getResult(), Org.class);
            if (CollectionUtils.isEmpty(orgList)) {
                throw new CallException("14010","找不到机构信息 by org_code=" + requestVo.getHospitalCode());
            }
            Org org = orgList.get(0);

            //出院小结入库
            outpatientSummary = new OutpatientSummary();
            outpatientSummary.setId(PKGenerator.generateId());
            //住院记录Id
            outpatientSummary.setId(requestVo.getPatientId());
            //住院日期
            outpatientSummary.setInDate(outSummary.getInDate());
            //出院日期
            outpatientSummary.setOutDate(outSummary.getOutDate());
            //手术名称
            outpatientSummary.setSurgeryName(outSummary.getSurgeryName());
            // 入院时情况
            outpatientSummary.setAdmissionConditions(outSummary.getAdmissionConditions());
            outpatientSummary.setSurgeryDate(outSummary.getSurgeryDate());
            //入院诊断
            outpatientSummary.setAdmissionDiagnosis(outSummary.getAdmissionDiagnosis());
            //出院诊断
            outpatientSummary.setDischargeConditions(outSummary.getDischargeConditions());
            //出院医嘱
            outpatientSummary.setDischargePrescription(outSummary.getDischargePrescription());
            //伤口愈合
            outpatientSummary.setWoundHealing(outSummary.getWoundHealing());
            //住院经过
            outpatientSummary.setOnSituation(outpatientSummary.getOnSituation());

            //住院次数
            outpatientSummary.setInTime(Integer.parseInt(requestVo.getInTime()));
            outpatientSummary.setAdmissionNo(requestVo.getAdmissionNo());

//            //病人基本信息
            outpatientSummary.setPatId(medicalCard ==null?"":medicalCard.getPatId());
            outpatientSummary.setPatName(medicalCard ==null?"":medicalCard.getPatName());
//
//            //组织信息
            outpatientSummary.setOrgId(org.getId());
            outpatientSummary.setOrgName(org.getOrgName());
              outpatientSummary.setOrgCode(requestVo.getHospitalCode());
            outpatientSummary.setBranchCode("");
            outpatientSummary.setBranchId("");
            outpatientSummary.setDeptName(outpatientSummary.getDeptName());
            outpatientSummary.setCreatedTime(DateUtils.getDatetime());

        }
    }
}
